package com.lantosec.hmrp.modules.morality.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.shiro.TokenUtil;
import com.lantosec.hmrp.core.util.DateUtils;
import com.lantosec.hmrp.modules.eval.vo.EvalItemVO;
import com.lantosec.hmrp.modules.morality.entity.*;
import com.lantosec.hmrp.modules.morality.mapper.MoralityNoteMapper;
import com.lantosec.hmrp.modules.morality.service.IMoralityItemService;
import com.lantosec.hmrp.modules.morality.service.IMoralityLevelService;
import com.lantosec.hmrp.modules.morality.service.IMoralityNoteSelfService;
import com.lantosec.hmrp.modules.morality.service.IMoralityNoteService;
import com.lantosec.hmrp.modules.morality.vo.MoralityNoteSelfVO;
import com.lantosec.hmrp.modules.morality.vo.MoralityNoteVO;
import com.lantosec.hmrp.modules.system.entity.SysDept;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 * 考评记录 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-04-20
 */
@Service
@Primary
@Log4j2
public class MoralityNoteServiceImpl extends ServiceImpl<MoralityNoteMapper, MoralityNote> implements IMoralityNoteService {
    @Autowired
    private IMoralityNoteSelfService iMoralityNoteSelfService;

    @Autowired
    private IMoralityItemService iMoralityItemService;

    @Autowired
    private IMoralityLevelService iMoralityLevelService;

    @Override
    public List<MoralityNoteVO> getAll(PageVO<MoralityNoteVO> page,Integer state,String moralityPlanName) {
        String userId = TokenUtil.getUserId();
        //查询考评记录基本信息
        List<MoralityNoteVO> moralityNoteVOList = baseMapper.getMoralityNote(page, userId, state, moralityPlanName);
        moralityNoteVOList.forEach(parm -> {
            //查询被考评人所在的科室
            String moralityTagetId = parm.getMoralityTargetId();
            SysDept sysDept = baseMapper.getDeptById(moralityTagetId);
            if(Objects.nonNull(sysDept)){
                parm.setDeptName(sysDept.getDeptName());
                parm.setTargetDeptId(sysDept.getDeptId());
            }
            //查询考评项目
            Map<String,Object> map = this.getMoralityNoteSelfVOList(parm);
            parm.setMap(map);
        });
        page.setRecords(moralityNoteVOList);
        return moralityNoteVOList;
    }

    /**
     * 查询考评项目
     * @param parm
     * @return
     */
    private Map<String,Object> getMoralityNoteSelfVOList(MoralityNoteVO parm) {
        Map<String,Object> map = new HashMap<>();
        //查询考评记录考评对象的父类项目
        List<MoralityNoteSelfVO> moralityNoteSelfVOList = baseMapper.getMoralityNoteSelf(parm.getMoralityNoteId(), parm.getMoralityTargetId(), parm.getMoralityPlanId());
        List<MoralityNoteSelfVO> moralityNoteSelfVO = new ArrayList<>();
        moralityNoteSelfVOList.forEach(parm1 -> {
            //当项目为基础项目时
            if (parm1.getMoralityItemType() == 1) {
                //查询父类项目名称
                String moralityItemId = parm1.getParentItemId();
                if(StringUtils.isNotEmpty(moralityItemId)){
                    MoralityItem moralityItem = iMoralityItemService.getById(moralityItemId);
                    if(Objects.nonNull(moralityItem)){
                        parm1.setName(moralityItem.getMoralityItemName());
                    }
                }
                //查询考评记录考评对象的子类项目
                List<MoralityNoteSelfVO> moralityNoteSelfVOList1 = baseMapper.getMoralityNoteSelf1(parm.getMoralityNoteId(), parm.getMoralityTargetId(),
                        parm.getMoralityPlanId(), moralityItemId);
                if(CollectionUtils.isNotEmpty(moralityNoteSelfVOList1)){
                    parm1.setMoralityNoteSelfVOList(moralityNoteSelfVOList1);
                }
                moralityNoteSelfVO.add(parm1);
            }
        });

        map.put("moralityNoteSelfVO",moralityNoteSelfVO);
        //查询当前用户的加减分项目
        //查询考评方案的考评周期加减分开始结束时间
        MoralityCycle moralityCycle = baseMapper.getMoralityCycle(parm.getMoralityPlanId());
        //获取当前用户id和名称
        String userId = TokenUtil.getUserId();
        String startDate = moralityCycle.getRewardStartDate();
        String endDate = moralityCycle.getRewardEndDate();
        List<EvalItemVO> evalItemVOS = baseMapper.getMoralityItem(startDate,endDate,userId,parm.getMoralityPlanId());
        map.put("evalItemVOS",evalItemVOS);
        return map;
    }

    @Override
    public void updateMoralityNote(MoralityNoteList moralityNoteList) {
        //修改考评记录基本信息
        MoralityNote moralityNote = moralityNoteList.getMoralityNote();
        moralityNote.setSelfCommitTime(DateUtils.nowDate());
        //计算得分 自评分+日常加减法
        /*BigDecimal selfScore = moralityNote.getSelfScore();
        BigDecimal rewardScore = moralityNote.getRewardScore();
        if(null != rewardScore){
            moralityNote.setSelfScore(selfScore.add(rewardScore));
        }*/
        this.updateById(moralityNote);

        //修改个人考评得分
        List<MoralityNoteSelf> moralityNoteSelfList = moralityNoteList.getMoralityNoteSelfList();
        moralityNoteSelfList.forEach(parm -> {
            //通过morality_note_id/morality_taget_id/morality_item_id修改项目得分
            iMoralityNoteSelfService.updateById(parm);
        });
    }

    @Override
    public List<MoralityNoteVO> getDeptNotExamine(PageVO<MoralityNoteVO> page,String userName,String moralityPlanName) {
        String userId = TokenUtil.getUserId();
        //查询考评记录基本信息
        List<MoralityNoteVO> moralityNoteVOList = baseMapper.getDeptNotExamine(page, userId,userName,moralityPlanName);
        moralityNoteVOList.forEach(parm -> {
            //查询出个人自评等级名称
            Integer selfLevel = parm.getSelfLevel();
            if(null != selfLevel){
                MoralityLevel moralityLevel = iMoralityLevelService.getById(selfLevel);
                String selfLevelName = moralityLevel.getMoralityLevelName();
                if(StringUtils.isNotEmpty(selfLevelName)){
                    parm.setSelfLevelName(selfLevelName);
                }
            }
            //查询考评项目
            Map<String,Object> map = getMoralityNoteSelfVOList(parm);
            parm.setMap(map);
        });
        page.setRecords(moralityNoteVOList);
        return moralityNoteVOList;
    }

    @Override
    public List<MoralityNoteVO> getDeptExamineDoc(PageVO<MoralityNoteVO> page) {
        //查询考评记录基本信息
        List<MoralityNoteVO> moralityNoteVOList = baseMapper.getDeptExamineDoc(page);
        return getMoralityNoteVOS(page, moralityNoteVOList);
    }

    @Override
    public List<MoralityNoteVO> getDeptExamineNur(PageVO<MoralityNoteVO> page) {
        //查询考评记录基本信息
        List<MoralityNoteVO> moralityNoteVOList = baseMapper.getDeptExamineNur(page);
        return getMoralityNoteVOS(page, moralityNoteVOList);
    }

    private List<MoralityNoteVO> getMoralityNoteVOS(PageVO<MoralityNoteVO> page, List<MoralityNoteVO> moralityNoteVOList) {
        moralityNoteVOList.forEach(parm -> {
            //查询考评项目
            Map<String, Object> map = getMoralityNoteSelfVOList(parm);
            parm.setMap(map);
        });
        page.setRecords(moralityNoteVOList);
        return moralityNoteVOList;
    }

    @Override
    public void deptExamineCommit(MoralityNoteList moralityNoteList) {
        //修改考评记录基本信息
        MoralityNote moralityNote = moralityNoteList.getMoralityNote();
        //修改考评提交时间
        updateMoralityNote(moralityNote);
    }

    /**
     * 修改考评提交时间
     * @param moralityNote
     */
    private void updateMoralityNote(MoralityNote moralityNote) {
        Date selfCommitTime = moralityNote.getSelfCommitTime();
        Date deptCommitTime = moralityNote.getDeptCommitTime();
        Date branchCommitTime = moralityNote.getBranchCommitTime();
        Date committeeCommitTime = moralityNote.getCommitteeCommitTime();
        if(null != selfCommitTime && null == deptCommitTime){
            moralityNote.setDeptCommitTime(DateUtils.nowDate());
        }
        if(null != deptCommitTime && null == branchCommitTime){
            moralityNote.setBranchCommitTime(DateUtils.nowDate());
        }
        if(null != branchCommitTime && null == committeeCommitTime){
            moralityNote.setCommitteeCommitTime(DateUtils.nowDate());
        }
        this.updateById(moralityNote);
    }

    @Override
    public List<MoralityNoteVO> getPartyBranchExamine(PageVO<MoralityNoteVO> page,String userName,String deptId,String moralityPlanName) {
        //获得当前用户的部门ID
        String deptIds = TokenUtil.getDeptId();
        String branchId = baseMapper.getMoralityBranchDept(deptIds);
        //查询考评记录基本信息
        List<MoralityNoteVO> moralityNoteVOList = baseMapper.getPartyBranchExamine(page,userName,deptId,branchId,moralityPlanName);
        moralityNoteVOList.forEach(parm -> {
            //查询科室考评等级名称
            Integer deptLevel = parm.getDeptLevel();
            if(null != deptLevel){
                MoralityLevel moralityLevel = iMoralityLevelService.getById(deptLevel);
                String deptLevelName = moralityLevel.getMoralityLevelName();
                if(StringUtils.isNotEmpty(deptLevelName)){
                    parm.setDeptLevelName(deptLevelName);
                }
            }
            //查询考评项目
            Map<String,Object> map = getMoralityNoteSelfVOList(parm);
            parm.setMap(map);
        });
        page.setRecords(moralityNoteVOList);
        return moralityNoteVOList;
    }

    @Override
    public List<MoralityNoteVO> getAppraisalExamine(PageVO<MoralityNoteVO> page,String userName,String deptId,String moralityPlanName) {
        //查询考评记录基本信息
        List<MoralityNoteVO> moralityNoteVOList = baseMapper.getAppraisalExamine(page,moralityPlanName,userName,deptId);
        moralityNoteVOList.forEach(parm -> {
            //查询党支部考评等级名称
            Integer branchLevel = parm.getBranchLevel();
            if(null != branchLevel){
                MoralityLevel moralityLevel = iMoralityLevelService.getById(branchLevel);
                String branchLevelName = moralityLevel.getMoralityLevelName();
                if(StringUtils.isNotEmpty(branchLevelName)){
                    parm.setBranchLevelName(branchLevelName);
                }
            }
            //查询考评项目
            Map<String,Object> map = getMoralityNoteSelfVOList(parm);
            parm.setMap(map);
        });
        page.setRecords(moralityNoteVOList);
        return moralityNoteVOList;
    }

    @Override
    public void deptExamineListCommit(List<MoralityNote> moralityNotes) {
        moralityNotes.forEach(param->{
            updateMoralityNote(param);
        });
    }

    @Override
    public void deptExamineAppCommit(MoralityNoteList moralityNoteList) {
        //修改考评记录基本信息
        MoralityNote moralityNote = moralityNoteList.getMoralityNote();
        Integer state = moralityNoteList.getState();
        if(state == 1){
            moralityNote.setDeptCommitTime(DateUtils.nowDate());
        }
        if(state == 2){
            moralityNote.setBranchCommitTime(DateUtils.nowDate());
        }
        if(state == 3){
            moralityNote.setCommitteeCommitTime(DateUtils.nowDate());
        }
        this.updateById(moralityNote);
    }
}
